package itsm.isperp.module.controller.app;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.entity.app.AppMessageRemind;
import itsm.isperp.module.service.app.AppMessageRemindService;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 系统帮助的控制器
 * 
 * @author lizx
 * @date 2013-7-27
 */
@Controller
@RequestMapping("app/message/remind")
public class AppMessageRemindController {

	@Autowired
	protected AppMessageRemindService appMessageRemindService;

	// ~-----------------------信箱---------------------------------

	/**
	 * 根据业务源id获取关联消息列表
	 * 
	 * @param sourceId
	 *            业务源id
	 * @param req
	 *            数据请求
	 * @return 列表json数据
	 */
	@RequestMapping("listBySourceId")
	@ResponseBody
	public List<AppMessageRemind> listBySourceId(String sourceId,
			JqGridRequest req1) {
		if (StringUtils.isEmpty(sourceId)) {
			return null;
		}else
		{
		  req1.addFilterRule("source_id", JqGridFilterRuleOp.eq, sourceId);
		}
		req1.isPageable(false);
		req1.setSort("create_date", Direction.DESC);
		return appMessageRemindService.findAll(req1).getContent();
	}

	/**
	 * 新建消息入口
	 */
	@RequestMapping("msgRemindEdit")
	public ModelAndView sendEdit(HttpServletRequest req) throws Exception {
		ModelAndView mv = new ModelAndView("app/message/remind/msgRemindEdit");
		String sourceType = req.getParameter("type");
		String sourceId = req.getParameter("sourceId");
		AppMessageRemind model = new AppMessageRemind();
		model.setSourceType(sourceType);
		model.setSourceId(sourceId);
		JqGridRequest req1 = new JqGridRequest();
		req1.isPageable(false);
		req1.setSort("create_date", Direction.DESC);
		req1.addFilterRule("source_id", JqGridFilterRuleOp.eq, sourceId);
		List<AppMessageRemind> lr = appMessageRemindService.findAll(req1)
				.getContent();
		mv.addObject("model", model);
		mv.addObject("arMsgList", lr);
		return mv;
	}

	@RequestMapping("save")
	@ResponseBody
	public ResultMessage save(AppMessageRemind model, HttpServletRequest req) {
		this.appMessageRemindService.saveOrUpdate(model);
		this.appMessageRemindService.updateBusinessMsgRemind(model);
		return ResultMessage.success(model.getId());
	}

	/**
	 * 关联消息
	 */
	@RequestMapping("relationMessageView")
	public ModelAndView relationMessageView(String sourceId, String all,
			HttpServletRequest req) {
		ModelAndView mv = new ModelAndView("app/message/relationMessageView");
		mv.addObject("sourceId", sourceId);
		mv.addObject("all", all);
		return mv;
	}

	@RequestMapping("findCountsBySourceIds")
	@ResponseBody
	public List<Map<String, Object>> findCountsBySourceIds(String all,
			HttpServletRequest req) {

		String ids = req.getParameter("ids");
		if (StringUtils.isEmpty(ids)) {
			return null;
		}
		String[] sourceIds = ids.substring(1).split(",");

		boolean a = false;
		if ("1".equals(all)) {
			a = true;
		}

		/*
		 * List<Map<String, Object>> l = this.appMessageRemindService
		 * .findRelationCountsBySourceIds(sourceIds, a);
		 */
		return null;
	}
}
